package com.game.controller;


import com.game.pojo.po.ActivationCode;
import com.game.pojo.po.ChatRecord;
import com.game.pojo.vo.Result;
import com.game.service.ChatRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lvhao
 * @since 2020-09-17
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
@Api(tags = "聊天记录")
public class ChatRecordController {

    @Autowired
    private ChatRecordService chatRecordService;

    @GetMapping("/chat_record/{num}")
    @ApiOperation("查询最近n条记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 202, message = "查询失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result getById(@PathVariable("num") int num){
        try{
            if(!chatRecordService.getByNumAndTime(num).isEmpty()){
                return new Result(200,"查询成功",chatRecordService.getByNumAndTime(num));
            }else{
                return new Result(202,"查询失败","");
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }

    @GetMapping("/chat_records")
    @ApiOperation("获取列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "未知错误")
    })
    public Result getAll(){
        try{
            return new Result(200,"查询成功",chatRecordService.list());
        }catch (Exception e){
            return new Result(500,"未知错误","服务器发生错误");
        }
    }

    @PostMapping("/chat_record")
    @ApiOperation("新建")
    @ApiResponses({
            @ApiResponse(code = 200, message = "新建成功"),
            @ApiResponse(code = 202, message = "新建失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result add(ChatRecord chatRecord){
        try{
            chatRecord.setTime(LocalDateTime.now().toString());
            if (chatRecordService.save(chatRecord)) {
                return new Result(200,"新建成功",chatRecord);
            }else{
                return new Result(202,"新建失败",chatRecord);
            }
        }catch (Exception e){
            return new Result(400,"参数有误","请求参数有误");
        }
    }

}

